package com.kaigejava.demo.business.barrgelog.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.kaigejava.demo.business.barrgelog.dto.BarrageLogDTO;
import com.kaigejava.demo.business.barrgelog.entity.BarrageLogEntity;
import com.kaigejava.demo.business.barrgelog.mapper.BarrageLogMapper;
import com.kaigejava.demo.business.barrgelog.service.IBarrageLogService;
import com.kaigejava.demo.business.enums.TypeEnum;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 凯哥Java
 * @description
 * @company
 * @since 2023/11/12 22:25
 */
@Controller
@RequestMapping("barrage-log")
public class BarrageLogController {


    @Resource
    private IBarrageLogService barrageLogService;
    @Resource
    private BarrageLogMapper barrageLogMapper;

    @RequestMapping("page-list")
    public String pageList(String typeCode, Model model) {
        List<BarrageLogEntity> list = barrageLogService.selectByCode(typeCode);
        List<BarrageLogDTO> dtos = Lists.newArrayList();
        for (BarrageLogEntity barrageLogEntity : list) {
            BarrageLogDTO dto = new BarrageLogDTO();
            BeanUtil.copyProperties(barrageLogEntity, dto);
            dtos.add(dto);
        }
        model.addAttribute("list", dtos);
        return "barrage-list";
    }

    @RequestMapping("/list")
    public String getList(Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn, String typeCode) {
        //分页查询数据，每页显示5条，
        Page<BarrageLogEntity> pageInfo = new Page<>(pn, 10);
        //分页查询的结果
        PageInfo<BarrageLogEntity> page = barrageLogService.selectPage(pageInfo, typeCode);
        model.addAttribute("typeCode", typeCode);
        model.addAttribute("page", page);
        model.addAttribute("types", TypeEnum.values());

        return "page.html";
    }

}
